/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: figure_03_phi_pop.do
Date: October 2022

Description: Reproduces Figure 3: Development accounting: firm-embedded productivity residualized by population.

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"

*Log
cap log close
log using "${clogs}/figure_03_phi_pop.log",replace
*-------------------------------------------------------------------------------



clear all
use "${data}/estimates_agg_naics_sales_s1_base_woparent.dta", clear

local grvar0 "gdp_pw_ppp" 
local grvar1 ""	
local var3 "A"
local yy "2016"
merge m:1 year isocode type using "${data}/aggregates_tfp_lp_klems_agg.dta" 
drop if _merge==2
drop _merge

foreach var in D_A  {
gen `var'_phi=(-1)*`var'*(0.2)
}

local LHS1 D_`var3'_phi
local LHS2 ln_`grvar0'
local RHS ln_pop


reg `LHS1' `LHS2' `RHS'
mat b = e(b)
mat V = e(V)
global c1: display %-04.2fc round(b[1,1],0.0001)
global se1: display %-04.2fc round(sqrt(V[1,1]),0.0001)


reg `LHS1' `RHS', nocon
mat b = e(b)
mat V = e(V)
global c_first: display %-03.2fc round(b[1,1],0.0001)
global se_first: display %-03.2fc round(sqrt(V[1,1]),0.0001)
predict lhs, res

reg `LHS2' `RHS', nocon 
mat b = e(b)
mat V = e(V)
global c_second: display %-03.2fc round(b[1,1],0.0001)
global se_second: display %-03.2fc round(sqrt(V[1,1]),0.0001)
predict rhs, res

reg lhs rhs 
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.01)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.01)
display "$c1"


*Figure 
*--------------------------------------------------------------------
local ytitle "{&Delta}`=ustrunescape("\u03D5\u0305")'{sub:n}"
local xtitle "{&Delta}y{sub:n}"

global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(small) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(small) mlabsize(small)
two (scatter lhs rhs, sort $opt1  mcolor(red)) (lfit lhs rhs, lcolor(red)) (line lhs lhs, lcolor(none)) ,  ylabel(-0.4(0.2)0.2, format(%5.1f) labsize(medlarge)) xlabel(-1(0.2)0.4, format(%5.1f) labsize(medlarge)) ///
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "`ytitle': $c1 ($se1)") rows(1) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

graph export "${rmain}/fig_03.pdf", replace

log close

	

	
	
	
	
	